package com.example.billbillbangumi;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.File;

public class Video extends AppCompatActivity implements View.OnClickListener{
    private VideoView videoView;
    private String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        Intent intent=getIntent();
        name=intent.getStringExtra("name");
        videoView=(VideoView)findViewById(R.id.video_view);
        Button play=(Button)findViewById(R.id.play);
        Button pause=(Button)findViewById(R.id.pause);
        Button replay=(Button)findViewById(R.id.replay);
        play.setOnClickListener(this);
        pause.setOnClickListener(this);
        replay.setOnClickListener(this);
        if(ContextCompat.checkSelfPermission(Video.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(Video.this,new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
        }else{
            initVideoPath();
        }
    }
    private void initVideoPath() {
        if (name.equals("入间同学入魔了")) {
            File file = new File(Environment.getExternalStorageDirectory(), "pv1.mp4");
            videoView.setVideoPath(file.getPath());
        }else if(name.equals("厨病激发男孩")){
            File file = new File(Environment.getExternalStorageDirectory(), "pv2.mp4");
            videoView.setVideoPath(file.getPath());
        }else if(name.equals("Dr.STONE石纪元")){
            File file = new File(Environment.getExternalStorageDirectory(), "pv3.mp4");
            videoView.setVideoPath(file.getPath());
        }else if(name.equals("花牌情缘3")){
            File file = new File(Environment.getExternalStorageDirectory(), "pv4.mp4");
            videoView.setVideoPath(file.getPath());
        }else if(name.equals("放学后桌游俱乐部")){
            File file = new File(Environment.getExternalStorageDirectory(), "pv5.mp4");
            videoView.setVideoPath(file.getPath());
        }
    }

    public void onRequestPermissonsResult(int requestCode,String[] permissions,int[] grantResults){
        switch (requestCode){
            case 1:
                if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    initVideoPath();
                }else {
                    Toast.makeText(this,"拒绝权限将无法使用程序",Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
            default:
        }
    }
    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.play:
                if(!videoView.isPlaying()){
                    videoView.start();
                }
                break;
            case R.id.pause:
                if(videoView.isPlaying()){
                    videoView.pause();
                }
                break;
            case R.id.replay:
                if(videoView.isPlaying()){
                    videoView.resume();
                }
                break;
        }
    }

    protected void onDestory(){
        super.onDestroy();
        if(videoView!=null){
            videoView.suspend();
        }
    }
}
